<?php
namespace Home\Controller;
use Think\Controller;
class ShopController extends Controller {
	
	public function __construct() {
		parent::__construct();
		$ti = M('ti') -> where("user_id = '".$_SESSION['user']['id']."'") -> find();
		
		if(empty($ti) || empty($_SESSION['user']['id']))
		{
			$this->redirect('Home/Login/login');
		}
    }
	
    public function index(){
		$pageNo=empty($_REQUEST['pageNo'])?1:$_REQUEST['pageNo'];
		$pageSize=empty($_REQUEST['pageSize'])?10:$_REQUEST['pageSize'];
		$shop_name=empty($_REQUEST['shopname'])?'':trim($_REQUEST['shopname']);
		$name=empty($_REQUEST['name'])?'id':$_REQUEST['name'];
		$asc=empty($_REQUEST['asc'])?'desc':trim($_REQUEST['asc']);
		
		
		$shop =D('shop'); 
		$result=$shop->shop_list($pageNo,$pageSize,$shop_name,$name,$asc);
		
		$this->assign("shop_list",$result['shop_list']);
		$this->assign("count",$result['count']);
		$this->assign("data",$result['data']);
		$this->assign("jumpurl",$_SERVER['HTTP_REFERER']);
		$this->display();
    }
	
	public function shop_change()
	{
		$id=empty($_REQUEST['id'])?'':$_REQUEST['id'];
		$data['shop_name']=empty($_REQUEST['shop_name'])?'':trim($_REQUEST['shop_name']);
		$data['appkey']=empty($_REQUEST['appkey'])?'':trim($_REQUEST['appkey']);
		$data['secretkey']=empty($_REQUEST['secretKey'])?'':trim($_REQUEST['secretKey']);
		$data['sessionkey']=empty($_REQUEST['sessionKey'])?'':trim($_REQUEST['sessionKey']);
		
		$jumpurl=empty($_REQUEST['jumpurl'])?'index':trim($_REQUEST['jumpurl']);
		
		$shop=D('shop');
		$res=$shop->shop_change($data,$id);
		
		$this->redirect('index');
	}
	
	/* 双击修改 */
	public function shop_edit()
	{
		$visitor_id = $_SESSION['user']['id'];  
		$id=empty($_REQUEST['id'])?'':$_REQUEST['id'];
		$name=empty($_REQUEST['name'])?'':$_REQUEST['name'];
		$value=empty($_REQUEST['value'])?'':$_REQUEST['value'];
		
		$data[$name]=$value;
		$shop=M('shop');
		$res=$shop->where("id='$id' and visitor_id = '$visitor_id'")->save($data);
		if($res!==false)
		{
			$msg='success';
		}
		else
		{
			$msg='fail';
		}
		
		echo $msg;
	}
	
	public function shop_delete()
	{
		$visitor_id = $_SESSION['user']['id']; 
		$id=empty($_REQUEST['id'])?'':$_REQUEST['id'];
		
		$shop=M('shop');
		$res=$shop->where("id='$id' and visitor_id = '$visitor_id'")->delete();
		   
		header("Location:".$_SERVER['HTTP_REFERER']);
	}  
	
	
}
















